package com.peza.bikelog.common.domain.polar;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import com.peza.bikelog.common.util.Util;

public class HrmHrThresholdSummary implements Writable{

	private static Util util = Util.getInstance();
			
	private List<Selection> selections;
	
	// Row 1
	private int totalTimeSelection;
	private int timeHrAboveMax;
	private int timeHrUth_Max;
	private int timeHrLth_Uth;
	private int timeHrRest_Lth;
	private int timeHrBelowRest;
	
	// Row 2
	private int maxHr;
	private int upperThreshold;	// anaerobic
	private int lowerThreshold; // aerobic
	private int restHr;
	
	
	public HrmHrThresholdSummary(){}
	
	public HrmHrThresholdSummary(String data){
		
		List<String>lines = Arrays.asList(data.split("\n"));
		Iterator<String> iter = lines.iterator();
		selections = new ArrayList<Selection>();
		
		// Row 1
		String row = iter.next();
		String [] cells = row.split("\t");
		setTotalTimeSelection(util.convertToInt(cells[0]));
		setTimeHrAboveMax(util.convertToInt(cells[1]));
		setTimeHrUth_Max(util.convertToInt(cells[2]));
		setTimeHrLth_Uth(util.convertToInt(cells[3]));
		setTimeHrRest_Lth(util.convertToInt(cells[4]));
		setTimeHrBelowRest(util.convertToInt(cells[5]));
		
		// Row 2
		row = iter.next();
		cells = row.split("\t");
		setMaxHr(util.convertToInt(cells[0]));
		setUpperThreshold(util.convertToInt(cells[1]));
		setLowerThreshold(util.convertToInt(cells[2]));
		setRestHr(util.convertToInt(cells[3]));
		
		// Row 3-n
		while(iter.hasNext()){
			Selection sel = new Selection();
			String row3 = iter.next();
			cells = row3.split("\t");
			sel.setStartSample(util.convertToInt(cells[0]));
			sel.setEndSample(util.convertToInt(cells[1]));
			
			selections.add(sel);
		}
	}

	
	private class Selection {
		
		// Row 3 
		private int startSample;
		private int endSample;

		
		public int getStartSample() {
			return startSample;
		}
		public void setStartSample(int startSample) {
			this.startSample = startSample;
		}
		public int getEndSample() {
			return endSample;
		}
		public void setEndSample(int endSample) {
			this.endSample = endSample;
		}
		
	}
	
	public List<Selection> getSelections() {
		return selections;
	}

	public void setSelections(List<Selection> selections) {
		this.selections = selections;
	}

	public int getTotalTimeSelection() {
		return totalTimeSelection;
	}
	public void setTotalTimeSelection(int totalTimeSelection) {
		this.totalTimeSelection = totalTimeSelection;
	}
	public int getTimeHrAboveMax() {
		return timeHrAboveMax;
	}
	public void setTimeHrAboveMax(int timeHrAboveMax) {
		this.timeHrAboveMax = timeHrAboveMax;
	}
	public int getTimeHrUth_Max() {
		return timeHrUth_Max;
	}
	public void setTimeHrUth_Max(int timeHrUth_Max) {
		this.timeHrUth_Max = timeHrUth_Max;
	}
	public int getTimeHrLth_Uth() {
		return timeHrLth_Uth;
	}
	public void setTimeHrLth_Uth(int timeHrLth_Uth) {
		this.timeHrLth_Uth = timeHrLth_Uth;
	}
	public int getTimeHrRest_Lth() {
		return timeHrRest_Lth;
	}
	public void setTimeHrRest_Lth(int timeHrRest_Lth) {
		this.timeHrRest_Lth = timeHrRest_Lth;
	}
	public int getTimeHrBelowRest() {
		return timeHrBelowRest;
	}
	public void setTimeHrBelowRest(int timeHrBelowRest) {
		this.timeHrBelowRest = timeHrBelowRest;
	}
	public int getMaxHr() {
		return maxHr;
	}
	public void setMaxHr(int maxHr) {
		this.maxHr = maxHr;
	}
	public int getUpperThreshold() {
		return upperThreshold;
	}
	public void setUpperThreshold(int upperThreshold) {
		this.upperThreshold = upperThreshold;
	}
	public int getLowerThreshold() {
		return lowerThreshold;
	}
	public void setLowerThreshold(int lowerThreshold) {
		this.lowerThreshold = lowerThreshold;
	}
	public int getRestHr() {
		return restHr;
	}
	public void setRestHr(int restHr) {
		this.restHr = restHr;
	}
	
	
	@Override
	public String toFileString() {
		StringBuffer buffer = new StringBuffer("[Summary-TH]\n");
		
		// Row 1
		buffer.append(totalTimeSelection +"\t"+ timeHrAboveMax +"\t"+ timeHrUth_Max +"\t");
		buffer.append(timeHrLth_Uth +"\t"+ timeHrRest_Lth +"\t"+ timeHrBelowRest +"\n");
		
		// Row 2
		buffer.append(maxHr +"\t"+ upperThreshold +"\t"+ lowerThreshold +"\t"+ restHr +"\n");
		
		// Row 3 
		for (Selection sel : selections){
			buffer.append(sel.getStartSample() +"\t"+ sel.getEndSample() +"\n");

		}
		buffer.append("\n");
		
		return buffer.toString();
	}			
}
